package io.chenglicun.modules.data.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.page.PageData;
import io.chenglicun.common.service.impl.BaseServiceImpl;
import io.chenglicun.modules.data.dao.DataGridVillageDao;
import io.chenglicun.modules.data.dao.DataHouseFacilitiesDao;
import io.chenglicun.modules.data.entity.DataGridVillageEntity;
import io.chenglicun.modules.data.entity.DataHouseFacilitiesEntity;
import io.chenglicun.modules.data.enums.DataGridVillageTypeEnums;
import io.chenglicun.modules.data.enums.DataHouseTypeEnums;
import io.chenglicun.modules.data.enums.FacilitiesAttributeEnums;
import io.chenglicun.modules.data.enums.FacilitiesTypeEnums;
import io.chenglicun.modules.data.service.DataGridVillageService;
import io.chenglicun.modules.data.service.DataHouseFacilitiesService;
import io.chenglicun.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Excel导入演示
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service
public class DataHouseFacilitiesServiceImpl extends BaseServiceImpl<DataHouseFacilitiesDao, DataHouseFacilitiesEntity> implements DataHouseFacilitiesService {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private DataGridVillageService dataGridVillageService;

    @Override
    public boolean excelData(List<List<Object>> list) {
        List<DataHouseFacilitiesEntity> insertList = new ArrayList<>();
        for (List<Object> o : list) {
            try {
                DataHouseFacilitiesEntity entity = new DataHouseFacilitiesEntity();
                if (o.size() > 1) {
                    String s = o.get(0) == null ? null : o.get(0).toString();//house_type//第一行房屋类型
                    if (s != null) {
                        DataHouseTypeEnums dataHouseTypeEnums = DataHouseTypeEnums.parseValue(s);
                        if (dataHouseTypeEnums != null) {
                            entity.setHouseType(dataHouseTypeEnums.getValue());
                        }
                    }
                }
                if (o.size() > 2) {
                    String facilitiesAttribute = o.get(1) == null ? null : o.get(1).toString();//facilities_attribute//第二行设施属性
                    if (facilitiesAttribute != null) {
                        FacilitiesAttributeEnums facilitiesAttributeEnums = FacilitiesAttributeEnums.parseValue(facilitiesAttribute);
                        if (facilitiesAttributeEnums != null) {
                            entity.setFacilitiesAttribute(facilitiesAttributeEnums.getValue());
                        }
                    }
                }

                if (o.size() > 3) {
                    String facilitiesType = o.get(2) == null ? null : o.get(2).toString();//facilities_type//第三行设施类型
                    if (facilitiesType != null) {
                        FacilitiesTypeEnums facilitiesTypeEnumsype = FacilitiesTypeEnums.parseValue(facilitiesType);
                        if (facilitiesTypeEnumsype != null) {
                            entity.setFacilitiesType(facilitiesTypeEnumsype.getValue());
                        }
                    }
                }
                if (o.size() > 4) {
                    String facilitiesName = o.get(3) == null ? null : o.get(3).toString();//facilities_type//第四行名称
                    entity.setFacilitiesName(facilitiesName);
                }
                if (o.size() > 5) {
                    String facilitiesHead = o.get(4) == null ? null : o.get(4).toString();//facilities_head//第五行设施负责人或者维护人员
                    entity.setFacilitiesHead(facilitiesHead);
                }

                if (o.size() > 6) {
                    String facilitiesHeadPhone = o.get(5) == null ? null : o.get(5).toString();//facilities_head_phone//第六行设施负责人或者维护人员电话
                    entity.setFacilitiesHeadPhone(facilitiesHeadPhone);
                }
                if (o.size() > 7) {
                    String facilitiesUse = o.get(6) == null ? null : o.get(6).toString();//facilities_use//第七行设施用途
                    entity.setFacilitiesUse(facilitiesUse);
                }
                if (o.size() > 8) {
                    String facilitiesImg = o.get(7) == null ? null : o.get(7).toString();//facilities_img//第八行设施照片
                    entity.setFacilitiesImg(facilitiesImg);
                }
                if (o.size() > 9) {
                    String facilitiesInformation = o.get(8) == null ? null : o.get(8).toString();//facilities_information//第九行设施信息
                    entity.setFacilitiesInformation(facilitiesInformation);
                }
                if (o.size() > 10) {
                    String facilitiesMonitor = o.get(9) == null ? null : o.get(9).toString();//facilities_monitor//第十行设施监控数据
                    entity.setFacilitiesMonitor(facilitiesMonitor);
                }
                if (o.size() > 11) {
                    String village = o.get(10) == null ? null : o.get(10).toString();//facilities_monitor//第十一行设施所属村落
                    DataGridVillageEntity dataGridVillageEntity = dataGridVillageService.queryDataGrid(village);
                    if (dataGridVillageEntity != null) {
                        entity.setVillageId(dataGridVillageEntity.getId());
                    }
                }

                entity.setCreator(SecurityUser.getUserId());
                entity.setCreateDate(new Date());
                entity.setUpdater(SecurityUser.getUserId());
                entity.setUpdaterDate(new Date());
                insertList.add(entity);

            } catch (Exception e) {
                logger.info("插入失败：" + JSONUtil.toJsonStr(o));
            }

        }
        super.insertBatch(insertList);
        return true;
    }


    @Override
    public PageData<DataHouseFacilitiesEntity> page(Map<String, Object> params) {
        IPage<DataHouseFacilitiesEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        PageData<DataHouseFacilitiesEntity> pageData = getPageData(page, DataHouseFacilitiesEntity.class);


        return pageData;
    }


    public void update(DataHouseFacilitiesEntity entity) {
        entity.setUpdater(SecurityUser.getUserId());
        entity.setUpdaterDate(new Date());
        super.updateById(entity);
    }

    public void delete(Integer[] ids) {
        super.deleteBatchIds(Arrays.asList(ids));
    }

    public void save(DataHouseFacilitiesEntity entity) {
        entity.setCreator(SecurityUser.getUserId());
        entity.setCreateDate(new Date());
        super.insert(entity);
    }


    public QueryWrapper<DataHouseFacilitiesEntity> getWrapper(Map<String, Object> params) {
        QueryWrapper<DataHouseFacilitiesEntity> wrapper = new QueryWrapper<>();

        String facilitiesName = (String) params.get("facilitiesName");
        wrapper.eq(StringUtils.isNotBlank(facilitiesName), "facilities_name", facilitiesName);
        String house_type = (String) params.get("houseType");
        wrapper.eq(StringUtils.isNotBlank(house_type), "type", house_type);
        wrapper.orderByDesc("create_date");
        return wrapper;
    }
}